<!DOCTYPE html>
<html lang="zh-CN,en,default">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.0.2">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"wakojam.gitee.io","root":"/","scheme":"Mist","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="最近一直工作中突然要用到P端交易实时监控，所以正好记录下碰到的问题">
<meta property="og:type" content="article">
<meta property="og:title" content="spring boot 整合druid">
<meta property="og:url" content="http://wakojam.gitee.io/2018/12/22/2018-12-22-springboot-druid-dbutils/index.html">
<meta property="og:site_name" content="Want笔记档案">
<meta property="og:description" content="最近一直工作中突然要用到P端交易实时监控，所以正好记录下碰到的问题">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2018-12-22T08:30:11.000Z">
<meta property="article:modified_time" content="2020-08-21T02:41:48.336Z">
<meta property="article:author" content="Want.Jocker">
<meta property="article:tag" content="spring boot">
<meta property="article:tag" content="druid">
<meta property="article:tag" content="dbutils">
<meta property="article:tag" content="Java">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://wakojam.gitee.io/2018/12/22/2018-12-22-springboot-druid-dbutils/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>spring boot 整合druid | Want笔记档案</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Want笔记档案</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

  <a href="https://gitee.com/WakoJam" class="github-corner" title="Follow me on Gitee" aria-label="Follow me on Gitee" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://wakojam.gitee.io/2018/12/22/2018-12-22-springboot-druid-dbutils/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Want.Jocker">
      <meta itemprop="description" content="不积跬步无以至千里">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Want笔记档案">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          spring boot 整合druid
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2018-12-22 16:30:11" itemprop="dateCreated datePublished" datetime="2018-12-22T16:30:11+08:00">2018-12-22</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-08-21 10:41:48" itemprop="dateModified" datetime="2020-08-21T10:41:48+08:00">2020-08-21</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/note/" itemprop="url" rel="index"><span itemprop="name">note</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <hr>
<p>需要实现的功能其实是比较简单的，就是在C端交易，通过P端转发，当该笔交易完成之后，P端进行数据库记录，并实时显示在银行监控端。</p>
<p>其实就是需要不断去数据库查询当前的是时候交易记录即可。</p>
<h2 id="1-druid"><a href="#1-druid" class="headerlink" title="1.druid"></a>1.druid</h2><p>参考文档：</p>
<p>[spring boot 开发druid 配置]<br><br>[1]: <a target="_blank" rel="noopener" href="https://yq.aliyun.com/articles/552746?spm=5176.10695662.1996646101.searchclickresult.757a1f46fqBXoN">https://yq.aliyun.com/articles/552746?spm=5176.10695662.1996646101.searchclickresult.757a1f46fqBXoN</a><br>[2]: <a target="_blank" rel="noopener" href="https://blog.csdn.net/scholar_man/article/details/78844172">https://blog.csdn.net/scholar_man/article/details/78844172</a></p>
<p>关于druid 有个问题一直都没想好怎么解决：正常连接启动之后，由于网络问题导致连接突然断开了，会直接报错，怎么可以把这种异常信息抓到，并显示通知页面客户</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">pom.xml 配置</span><br><span class="line"></span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;com.alibaba&lt;&#x2F;groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;druid&lt;&#x2F;artifactId&gt;</span><br><span class="line">    &lt;version&gt;$&#123;druid-version&#125;&lt;&#x2F;version&gt;</span><br><span class="line">&lt;&#x2F;dependency&gt;</span><br><span class="line"></span><br><span class="line">在进入druid之后，sql监控不能正常显示数据，在其他文章中也有看到使用下面这个，（这个主要还是跟配置的拦截器有关系），</span><br><span class="line">因为我这边基本上直接在java类中sql是固定的，所以这边也拦截不到其他的请求，所以数据一直都没有正常显示。个人理解，欢迎指正。</span><br><span class="line"></span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;com.alibaba&lt;&#x2F;groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;druid-spring-boot-starter&lt;&#x2F;artifactId&gt;</span><br><span class="line">    &lt;version&gt;1.1.1&lt;&#x2F;version&gt;</span><br><span class="line">&lt;&#x2F;dependency&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>application.properties</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">#DB2配置</span><br><span class="line">spring.datasource.type&#x3D;com.alibaba.druid.pool.DruidDataSource</span><br><span class="line">spring.datasource.driver-class-name&#x3D;com.ibm.db2.jcc.DB2Driver</span><br><span class="line">spring.datasource.url&#x3D;jdbc:db2:&#x2F;&#x2F;10.1.2.74:60052&#x2F;db2atm1</span><br><span class="line">spring.datasource.username&#x3D;db2atm1</span><br><span class="line">spring.datasource.password&#x3D;db2atm1</span><br><span class="line"></span><br><span class="line"># 下面为连接池的补充设置，应用到上面所有数据源中</span><br><span class="line"># 初始化大小，最小，最大</span><br><span class="line">spring.datasource.initialSize&#x3D;5</span><br><span class="line">spring.datasource.minIdle&#x3D;5</span><br><span class="line">spring.datasource.maxActive&#x3D;20</span><br><span class="line"># 配置获取连接等待超时的时间</span><br><span class="line">spring.datasource.maxWait&#x3D;60000</span><br><span class="line"># 配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒</span><br><span class="line">spring.datasource.timeBetweenEvictionRunsMillis&#x3D;60000</span><br><span class="line"># 配置一个连接在池中最小生存的时间，单位是毫秒</span><br><span class="line">spring.datasource.minEvictableIdleTimeMillis&#x3D;300000</span><br><span class="line">spring.datasource.validationQuery&#x3D;SELECT 1 FROM DUAL</span><br><span class="line">spring.datasource.testWhileIdle&#x3D;true</span><br><span class="line">spring.datasource.testOnBorrow&#x3D;false</span><br><span class="line">spring.datasource.testOnReturn&#x3D;false</span><br><span class="line"># 打开PSCache，并且指定每个连接上PSCache的大小</span><br><span class="line">spring.datasource.poolPreparedStatements&#x3D;true</span><br><span class="line">spring.datasource.maxPoolPreparedStatementPerConnectionSize&#x3D;20</span><br><span class="line"># 配置监控统计拦截的filters，去掉后监控界面sql无法统计，&#39;wall&#39;用于防火墙</span><br><span class="line">spring.datasource.filters&#x3D;stat,wall,log4j</span><br><span class="line"># 通过connectProperties属性来打开mergeSql功能；慢SQL记录</span><br><span class="line">spring.datasource.connectionProperties&#x3D;druid.stat.mergeSql&#x3D;true;</span><br><span class="line">druid.stat.slowSqlMillis&#x3D;5000</span><br><span class="line">spring.datasource.logSlowSql&#x3D;true</span><br><span class="line"># 合并多个DruidDataSource的监控数据</span><br><span class="line">spring.datasource.useGlobalDataSourceStat&#x3D;true</span><br><span class="line">spring.main.allow-bean-definition-overriding&#x3D;true</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>工具类：DruidUtils</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br></pre></td><td class="code"><pre><span class="line">package com.qhnx.Config;</span><br><span class="line"></span><br><span class="line">import com.alibaba.druid.pool.DruidDataSource;</span><br><span class="line">import com.alibaba.druid.support.http.StatViewServlet;</span><br><span class="line">import com.alibaba.druid.support.http.WebStatFilter;</span><br><span class="line">import org.slf4j.Logger;</span><br><span class="line">import org.slf4j.LoggerFactory;</span><br><span class="line">import org.springframework.beans.factory.annotation.Value;</span><br><span class="line">import org.springframework.boot.web.servlet.FilterRegistrationBean;</span><br><span class="line">import org.springframework.boot.web.servlet.ServletRegistrationBean;</span><br><span class="line">import org.springframework.context.annotation.Bean;</span><br><span class="line">import org.springframework.context.annotation.Configuration;</span><br><span class="line"></span><br><span class="line">import javax.sql.DataSource;</span><br><span class="line">import java.sql.SQLException;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">@Configuration</span><br><span class="line">public class DruidConfig &#123;</span><br><span class="line"></span><br><span class="line">    private Logger logger &#x3D; LoggerFactory.getLogger(DruidConfig.class);</span><br><span class="line"></span><br><span class="line">    @Value(&quot;$&#123;spring.datasource.url&#125;&quot;)</span><br><span class="line">    private String dbUrl;</span><br><span class="line"></span><br><span class="line">    @Value(&quot;$&#123;spring.datasource.username&#125;&quot;)</span><br><span class="line">    private String username;</span><br><span class="line"></span><br><span class="line">    @Value(&quot;$&#123;spring.datasource.password&#125;&quot;)</span><br><span class="line">    private String password;</span><br><span class="line"></span><br><span class="line">    @Value(&quot;$&#123;spring.datasource.driver-class-name&#125;&quot;)</span><br><span class="line">    private String driverClassName;</span><br><span class="line"></span><br><span class="line">    @Value(&quot;$&#123;spring.datasource.initialSize&#125;&quot;)</span><br><span class="line">    private int initialSize;</span><br><span class="line"></span><br><span class="line">    @Value(&quot;$&#123;spring.datasource.minIdle&#125;&quot;)</span><br><span class="line">    private int minIdle;</span><br><span class="line"></span><br><span class="line">    @Value(&quot;$&#123;spring.datasource.maxActive&#125;&quot;)</span><br><span class="line">    private int maxActive;</span><br><span class="line"></span><br><span class="line">    @Value(&quot;$&#123;spring.datasource.maxWait&#125;&quot;)</span><br><span class="line">    private int maxWait;</span><br><span class="line"></span><br><span class="line">    @Value(&quot;$&#123;spring.datasource.timeBetweenEvictionRunsMillis&#125;&quot;)</span><br><span class="line">    private int timeBetweenEvictionRunsMillis;</span><br><span class="line"></span><br><span class="line">    @Value(&quot;$&#123;spring.datasource.minEvictableIdleTimeMillis&#125;&quot;)</span><br><span class="line">    private int minEvictableIdleTimeMillis;</span><br><span class="line"></span><br><span class="line">    @Value(&quot;$&#123;spring.datasource.validationQuery&#125;&quot;)</span><br><span class="line">    private String validationQuery;</span><br><span class="line"></span><br><span class="line">    @Value(&quot;$&#123;spring.datasource.testWhileIdle&#125;&quot;)</span><br><span class="line">    private boolean testWhileIdle;</span><br><span class="line"></span><br><span class="line">    @Value(&quot;$&#123;spring.datasource.testOnBorrow&#125;&quot;)</span><br><span class="line">    private boolean testOnBorrow;</span><br><span class="line"></span><br><span class="line">    @Value(&quot;$&#123;spring.datasource.testOnReturn&#125;&quot;)</span><br><span class="line">    private boolean testOnReturn;</span><br><span class="line"></span><br><span class="line">    @Value(&quot;$&#123;spring.datasource.filters&#125;&quot;)</span><br><span class="line">    private String filters;</span><br><span class="line"></span><br><span class="line">    @Value(&quot;$&#123;spring.datasource.logSlowSql&#125;&quot;)</span><br><span class="line">    private String logSlowSql;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public ServletRegistrationBean druidServlet() &#123;</span><br><span class="line">        ServletRegistrationBean reg &#x3D; new ServletRegistrationBean();</span><br><span class="line">        reg.setServlet(new StatViewServlet());</span><br><span class="line">        reg.addUrlMappings(&quot;&#x2F;druid&#x2F;*&quot;);</span><br><span class="line">        reg.addInitParameter(&quot;loginUsername&quot;, username);</span><br><span class="line">        reg.addInitParameter(&quot;loginPassword&quot;, password);</span><br><span class="line">        reg.addInitParameter(&quot;logSlowSql&quot;, logSlowSql);</span><br><span class="line">        return reg;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public FilterRegistrationBean filterRegistrationBean() &#123;</span><br><span class="line">        FilterRegistrationBean filterRegistrationBean &#x3D; new FilterRegistrationBean();</span><br><span class="line">        filterRegistrationBean.setFilter(new WebStatFilter());</span><br><span class="line">        filterRegistrationBean.addUrlPatterns(&quot;&#x2F;*&quot;);</span><br><span class="line">        filterRegistrationBean.addInitParameter(&quot;exclusions&quot;, &quot;*.js,*.gif,*.jpg,*.png,*.css,*.ico,&#x2F;druid&#x2F;*&quot;);</span><br><span class="line">        filterRegistrationBean.addInitParameter(&quot;profileEnable&quot;, &quot;true&quot;);</span><br><span class="line">        return filterRegistrationBean;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public DataSource druidDataSource() &#123;</span><br><span class="line">        DruidDataSource datasource &#x3D; new DruidDataSource();</span><br><span class="line">        datasource.setUrl(dbUrl);</span><br><span class="line">        datasource.setUsername(username);</span><br><span class="line">        datasource.setPassword(password);</span><br><span class="line">        datasource.setDriverClassName(driverClassName);</span><br><span class="line">        datasource.setInitialSize(initialSize);</span><br><span class="line">        datasource.setMinIdle(minIdle);</span><br><span class="line">        datasource.setMaxActive(maxActive);</span><br><span class="line">        datasource.setMaxWait(maxWait);</span><br><span class="line">        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);</span><br><span class="line">        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);</span><br><span class="line">        datasource.setValidationQuery(validationQuery);</span><br><span class="line">        datasource.setTestWhileIdle(testWhileIdle);</span><br><span class="line">        datasource.setTestOnBorrow(testOnBorrow);</span><br><span class="line">        datasource.setTestOnReturn(testOnReturn);</span><br><span class="line">        try &#123;</span><br><span class="line">            datasource.setFilters(filters);</span><br><span class="line">        &#125; catch (SQLException e) &#123;</span><br><span class="line">            logger.error(&quot;druid configuration initialization filter&quot;, e);</span><br><span class="line">        &#125;</span><br><span class="line">        return datasource;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>如果整合成功，则可以直接进入 <a target="_blank" rel="noopener" href="http://localhost:8080/druid/weburi.html">http://localhost:8080/druid/weburi.html</a>  直接进行登录。用户名和密码则是配置文件中的数据库名和密码<br>如果不想使用，也可以在工具类中进行修改即可。</p>
<h1 id="2-Apach-dbutils"><a href="#2-Apach-dbutils" class="headerlink" title="2.Apach dbutils"></a>2.Apach dbutils</h1><p>参考文档：<br>[apach dbutils 配置]<br><br>[1]: <a target="_blank" rel="noopener" href="https://blog.csdn.net/earbao/article/details/44901061">https://blog.csdn.net/earbao/article/details/44901061</a><br>[2]: <a target="_blank" rel="noopener" href="https://blog.csdn.net/softwave/article/details/18604565">https://blog.csdn.net/softwave/article/details/18604565</a><br>[3]:<a target="_blank" rel="noopener" href="http://commons.apache.org/">http://commons.apache.org/</a></p>
<p>在网上查了好多种方式,感觉这个用起来更方便一点。直接贴代码：  </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;dependency&gt;</span><br><span class="line">   &lt;groupId&gt;commons-dbutils&lt;&#x2F;groupId&gt;</span><br><span class="line">   &lt;artifactId&gt;commons-dbutils&lt;&#x2F;artifactId&gt;</span><br><span class="line">   &lt;version&gt;1.6&lt;&#x2F;version&gt;</span><br><span class="line">&lt;&#x2F;dependency&gt;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">package com.qhnx.Config;</span><br><span class="line"></span><br><span class="line">import java.io.InputStream;</span><br><span class="line">import java.util.Properties;</span><br><span class="line">import javax.sql.DataSource;</span><br><span class="line"></span><br><span class="line">import com.alibaba.druid.pool.DruidDataSource;</span><br><span class="line">import com.alibaba.druid.pool.DruidDataSourceFactory;</span><br><span class="line"></span><br><span class="line">public class DruidUtils &#123;</span><br><span class="line">    private static DruidDataSource ds;</span><br><span class="line"></span><br><span class="line">    static &#123;</span><br><span class="line">        try &#123;</span><br><span class="line">            InputStream is &#x3D; DruidUtils.class.getClassLoader().getResourceAsStream(&quot;application.properties&quot;);</span><br><span class="line">            Properties properties &#x3D; new Properties();</span><br><span class="line">            properties.load(is);</span><br><span class="line"></span><br><span class="line">            ds &#x3D; (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);</span><br><span class="line">        &#125; catch (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static DataSource getDataSource() &#123;</span><br><span class="line">        if (ds !&#x3D; null) &#123;</span><br><span class="line">            return ds;</span><br><span class="line">        &#125;</span><br><span class="line">        return null;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><strong>在这个地方有个特别需要注意：读取数据源配置文件的时候，这边默认读取的名称为’driverClassName,url,username,password’,所以就算之前在整合druid已经配置了数据源，但是在这里建议重新进行如下配置</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">driverClassName&#x3D;com.ibm.db2.jcc.DB2Driver</span><br><span class="line">url&#x3D;jdbc:db2:&#x2F;&#x2F;10.1.2.74:60052&#x2F;db2atm1</span><br><span class="line">username&#x3D;db2atm1</span><br><span class="line">password&#x3D;db2atm1</span><br></pre></td></tr></table></figure>


<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line">package com.qhnx.dao.impl;</span><br><span class="line"></span><br><span class="line">import com.qhnx.Config.DruidUtils;</span><br><span class="line">import com.qhnx.dao.ITranRecordDao;</span><br><span class="line">import com.qhnx.model.monitor;</span><br><span class="line"></span><br><span class="line">import org.apache.commons.dbutils.*;</span><br><span class="line">import org.apache.commons.dbutils.handlers.BeanListHandler;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">import java.sql.SQLException;</span><br><span class="line"></span><br><span class="line">import java.util.List;</span><br><span class="line"></span><br><span class="line">public class TranRecordDaoImpl implements ITranRecordDao &#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#x2F;**</span><br><span class="line">     * 查找所有</span><br><span class="line">     *</span><br><span class="line">     * @return</span><br><span class="line">     *&#x2F;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line"></span><br><span class="line">    public List&lt;monitor&gt; findAll() &#123;</span><br><span class="line"></span><br><span class="line">        QueryRunner runner &#x3D; new QueryRunner(DruidUtils.getDataSource());</span><br><span class="line">        List&lt;monitor&gt;  list &#x3D; null;</span><br><span class="line"></span><br><span class="line">        try &#123;</span><br><span class="line">            String SQL &#x3D; &quot;select * from TRANREC_MONITOR fetch first 50 rows only&quot;;</span><br><span class="line">            list &#x3D; runner.query(SQL, new BeanListHandler&lt;&gt;(monitor.class));</span><br><span class="line">        &#125; catch (SQLException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        return list;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">说明：BeanListHandler 查询出多个结果集。</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h1 id="3-spring-boot"><a href="#3-spring-boot" class="headerlink" title="3. spring boot"></a>3. spring boot</h1><p>在这里只说明一个启动时报错的问题。由于当时没准备写这个说明，所以没有错误截图。</p>
<p>在启动的时候如果报错找不到配置文件，或者找不到数据源的配置文件，类似于这种情况，只要在启动项增加  </p>
<p>@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})</p>
<p>其他的错误问题都比较常见可以直接搜到。</p>
<p>最后说个数据源驱动的问题：<br>因为我这边服务器使用的db2数据库，自己电脑安装的mysql,无意中发现的问题。<br>mysql在使用驱动时需要注意，否则启动时会报错</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">在6.0.2版本之前</span><br><span class="line">driver&#x3D;com.mysql.jdbc.Driver    </span><br><span class="line">url&#x3D;jdbc:mysql:&#x2F;&#x2F;host:port&#x2F;dbname?characterEncoding&#x3D;utf8</span><br><span class="line"></span><br><span class="line">在6.0.2版本之后</span><br><span class="line">driver&#x3D;com.mysql.cj.jdbc.Driver</span><br><span class="line">url&#x3D;jdbc:mysql:&#x2F;&#x2F;host:port&#x2F;dbname?characterEncoding&#x3D;utf8&amp;serverTimezone&#x3D;UTC</span><br><span class="line"></span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;com.ibm.db2&lt;&#x2F;groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;db2jcc4&lt;&#x2F;artifactId&gt;</span><br><span class="line">    &lt;version&gt;10.5&lt;&#x2F;version&gt;</span><br><span class="line">&lt;&#x2F;dependency&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;com.ibm.db2&lt;&#x2F;groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;db2jcc_license_cu&lt;&#x2F;artifactId&gt;</span><br><span class="line">    &lt;version&gt;10.5&lt;&#x2F;version&gt;</span><br><span class="line">&lt;&#x2F;dependency&gt;</span><br><span class="line"></span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;mysql&lt;&#x2F;groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;mysql-connector-java&lt;&#x2F;artifactId&gt;</span><br><span class="line">&lt;&#x2F;dependency&gt;</span><br></pre></td></tr></table></figure>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/spring-boot/" rel="tag"># spring boot</a>
              <a href="/tags/druid/" rel="tag"># druid</a>
              <a href="/tags/dbutils/" rel="tag"># dbutils</a>
              <a href="/tags/Java/" rel="tag"># Java</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2018/12/13/2018-12-13-MySQL-%E7%94%A8%E6%88%B7%E5%88%9B%E5%BB%BA%E4%BB%A5%E5%8F%8A%E6%8E%88%E6%9D%83%E5%B9%B6%E8%A7%A3%E5%86%B3%E6%96%B0%E5%BB%BA%E7%94%A8%E6%88%B7%E6%97%A0%E6%B3%95%E7%99%BB%E5%BD%95%E9%97%AE%E9%A2%98/" rel="prev" title="MySQL 创建用户、授权并解决新建用户无法登录问题">
      <i class="fa fa-chevron-left"></i> MySQL 创建用户、授权并解决新建用户无法登录问题
    </a></div>
      <div class="post-nav-item">
    <a href="/2018/12/27/2018-12-27-LinuxOS-install-VSCod/" rel="next" title="CentOS 7.x系统安装Visual Studio Code">
      CentOS 7.x系统安装Visual Studio Code <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-druid"><span class="nav-number">1.</span> <span class="nav-text">1.druid</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#2-Apach-dbutils"><span class="nav-number"></span> <span class="nav-text">2.Apach dbutils</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#3-spring-boot"><span class="nav-number"></span> <span class="nav-text">3. spring boot</span></a></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Want.Jocker"
      src="/images/avatar.gif">
  <p class="site-author-name" itemprop="name">Want.Jocker</p>
  <div class="site-description" itemprop="description">不积跬步无以至千里</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">25</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">2</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">21</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Want.Jocker</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://mist.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
